package com.woniu113.junk.site.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class AddressUtil {

    private static final Logger log = LoggerFactory.getLogger(AddressUtil.class);

    public static String getLatAndLngByAddr(String addr) {
        try {
            String result = "";

            addr = addr.replace(" ", "").replace("#", "").replace("中国", "");
            String queryStr = "http://api.tianditu.gov.cn/geocoder?ds=%7B'keyWord':'" + addr + "'%7D&tk=" + "34b62d6f4c3f4edce54e9655133cfcce";
            String info = HttpClientUtil.get(queryStr);
            if (StringUtils.isNotBlank(info)) {
                JSONObject resultJson = JSON.parseObject(info);
                if (!"无结果".equals(resultJson.get("msg"))) {
                    JSONObject locationObj = (JSONObject) resultJson.get("location");
                    //纬度
                    String lat = locationObj.get("lat") + "";
                    //经度
                    String lng = locationObj.get("lon") + "";

                    System.out.println("纬度"+lat);
                    System.out.println("经度"+lng);

                    result = "经度：" + lng + "\n纬度：" + lat;
                    System.out.println(result);
                }
            }
            return result;
        } catch (Exception e) {
            return "无";
        }
    }

    public static void main(String[] args) {
        System.out.println(getLatAndLngByAddr("重庆市观音桥"));
    }

}